package com.bw.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.bw.entity.Dict;
import com.bw.entity.Employee;
import com.bw.entity.EmployeeAdd;
import com.bw.entity.EmployeeQuery;
import com.bw.mapper.EmployeeMapper;
import com.bw.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Controller
public class EmployeeController {

    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    EmployeeService employeeService;


    @RequestMapping("/list.do")
    public String list(EmployeeQuery query, Integer pageNum, Model model) {
        //1. 渲染下拉框
        List<Dict> deptList = employeeMapper.deptList();
        model.addAttribute("deptList", deptList);
        List<Dict> groupList = employeeMapper.groupList();
        model.addAttribute("groupList", groupList);
        //2. 分页查询

        PageHelper.startPage(pageNum == null ? 1 : pageNum, 3);
        List<Employee> employeeList = employeeMapper.selectByParam(query);
        PageInfo<Employee> pageInfo = new PageInfo<>(employeeList);

        model.addAttribute("pageInfo", pageInfo);


        return "list";

    }

    @RequestMapping("/del.do")
    public String del(String id){
        System.out.println("id ===========" + id);
        employeeMapper.delById(id);

        // 重定向
        return "redirect:list.do";
    }

    @RequestMapping("/addPage.do")
    public String addPage(Model model){
        //1. 渲染下拉框
        List<Dict> deptList = employeeMapper.deptList();
        model.addAttribute("deptList", deptList);
        List<Dict> groupList = employeeMapper.groupList();
        model.addAttribute("groupList", groupList);


        return "add";
    }
    @RequestMapping("/add.do")
    public String add(@Valid EmployeeAdd employeeAdd, BindingResult result, Model model){
        if (result.hasErrors()){
            model.addAttribute("error",result.getAllErrors().get(0).getDefaultMessage());
            //1. 渲染下拉框
            List<Dict> deptList = employeeMapper.deptList();
            model.addAttribute("deptList", deptList);
            List<Dict> groupList = employeeMapper.groupList();
            model.addAttribute("groupList", groupList);
            return "add";
        }
        //员工工号以“GF”开始
        if (!employeeAdd.getNum().startsWith("GF")){
            model.addAttribute("error","员工工号以“GF”开始");
            //1. 渲染下拉框
            List<Dict> deptList = employeeMapper.deptList();
            model.addAttribute("deptList", deptList);
            List<Dict> groupList = employeeMapper.groupList();
            model.addAttribute("groupList", groupList);
            return "add";
        }
        //工号不能重复
        Employee employee = employeeMapper.selectByNum(employeeAdd.getNum());
        if (employee != null){
            model.addAttribute("error","工号不能重复");
            //1. 渲染下拉框
            List<Dict> deptList = employeeMapper.deptList();
            model.addAttribute("deptList", deptList);
            List<Dict> groupList = employeeMapper.groupList();
            model.addAttribute("groupList", groupList);
            return "add";
        }
        //入职时间
        String createTime = employeeAdd.getCreateTime();
        DateTime dateTime = DateUtil.parse(createTime);
        //当前时间
        Date date = new Date();
        //入职时间小于当前时间
        if (dateTime.isAfter(date)){
            model.addAttribute("error","入职时间小于当前时间");
            //1. 渲染下拉框
            List<Dict> deptList = employeeMapper.deptList();
            model.addAttribute("deptList", deptList);
            List<Dict> groupList = employeeMapper.groupList();
            model.addAttribute("groupList", groupList);
            return "add";
        }

        employeeService.add(employeeAdd);

        return "redirect:list.do";
    }
}
